<?php
require_once 'login.php';
echo <<<_END

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js map-page"> <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Photogrammar: Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/css/styleMain.css"/>
    <link rel="stylesheet" href="/css/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.ie.css" /><![endif]-->
    <link  href="/css/cartodb-leaflet.css" rel="stylesheet" type="text/css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/js/leaflet.js"></script>
    <script type="text/javascript" src="/js/wax.leaf.min-6.2.3-touched.js"></script>
    <script type="text/javascript" src="/dist/cartodb-leaflet-min.js"></script>
    <script type="text/javascript" src="/dist/cartodb-popup-min.js"></script>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.google_menu.js"></script>
    
    <script>
            $('document').ready(function(){
                $('.menu').fixedMenu();
            });
    </script>
    
    <script type="text/javascript" src="../js/mapping.js"></script>
</head>
<body onload="initialize()">

<div id="header">
	<div id="logo">
    	<h1><a href="/"><img src="/images/photogrammar-wordmark.jpg" alt="Yale Photogrammar" title="Yale Photogrammar" /></a></h1>
    </div><!-- /#logo -->
    <nav id="menu" class="menu">
        <ul>
          <li class="single-link">
            <a href="/index.php">Home</a>
            </li>
          <li class="current">
            <a href="/map/">Map</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/browse/">Browse</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/search/">Search</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/About/">About</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/blog/">Blog</a>
            </li>
        </ul>
    </nav><!-- /#menu -->
</div><!-- /#header -->
<div class="searchmain">
_END;

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die("Unable to connect to MySQL: " .mysql_error());

mysql_select_db($db_database) or die('Unable to connect to MySQL: ' . mysql_error());

$fval = array('photographer'=>'', 'lotNum'=>'', 'secondPolitical'=>'', 'year'=>'', 'start'=>0);
echo <<<_END
<br/><center><p style="margin:1em"><h4>
<form action='asearch.php' method='get'>
<input type ="hidden" id="start" name="start" value=0> 
 &nbsp &nbsp &nbsp Photographer: <input type ="text" name="photographer" style="height: 40px; width: 300px; font: bold 24px Arial"/> &nbsp
Lot Number: <input type ="text" name="lotNum" style="height: 40px; width: 120px; font: bold 24px Arial" /> &nbsp
County: <input type ="text" name="secondPolitical" style="height: 40px; width: 200px; font: bold 24px Arial" /> &nbsp
Year: <select name="year" style="height: 40px; width: 120px; font: bold 24px Arial"> 
    <option value=''>All</option>
    <option value=1935>1935</option>
    <option value=1936>1936</option>
    <option value=1937>1937</option>
    <option value=1938>1938</option>
    <option value=1939>1939</option>
    <option value=1940>1940</option>
    <option value=1941>1941</option>
    <option value=1942>1942</option>
    <option value=1943>1943</option>
</select>
</h4></p>
<p><button type='submit' value='Search' style="padding: 5px 10px; height: 40px; width: 120px; font: bold 24px Arial"" />
Search</button></p>
</center>
</form>
_END;

if(isset($_GET['photographer']) && isset($_GET['lotNum']))
{
    $fval['photographer'] = $_GET['photographer'];
    $fval['lotNum'] = $_GET['lotNum'];
    $fval['secondPolitical'] = $_GET['secondPolitical'];
    $fval['year'] = $_GET['year'];
    $fval['start'] = $_GET['start'];

    $photo = get_post('photographer');
    //echo $photo;
    $query = "SELECT * FROM photo WHERE photographer LIKE '%" . get_post('photographer') . "%' " .
        "AND lotNum LIKE '%" . get_post('lotNum') . "%' " .
        "AND secondPolitical LIKE '%" . get_post('secondPolitical') . "%' " .
        "AND date LIKE '%" . get_post('year') . "%'" ;
    //echo $query;
    //echo '<br />';
    //$query = "SELECT * FROM photo";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());

    $rows = mysql_num_rows($result);
    echo ' <center> <h4>';
    echo $rows . ' total results found. &nbsp Showing photographs &nbsp ' . 
        (get_post('start') + 1)  . '-' . min(get_post('start') + 48, $rows)   . '</br>';

    //for Photographer:"' . get_post('photographer') . '", &nbsp Lot = "' .
        //get_post('lotNum') . '"' . '", &nbsp County = "' .
        //get_post('secondPolitical') . '"'  .  '", &nbsp Year = "' . get_post('year') . '"' ;
    //if($rows > 200) echo ' ; &nbsp Showing only first 200 results';
    echo '<h2>';
    if(get_post('start') != 0) {
        $query_arr = $_GET;
        $query_arr["start"] = max($query_arr["start"] - 48, 0);
        $query_call = http_build_query($query_arr);
        echo '<a href="' . 'http://photogrammar.yale.edu/asearch.php?' . $query_call . '"> previous &nbsp &nbsp </a>';
    }
    if(get_post('start') + 48 < $rows) {
        $query_arr = $_GET;
        $query_arr["start"] = $query_arr["start"] + 48;
        $query_call = http_build_query($query_arr);
        echo '<a href="' . 'http://photogrammar.yale.edu/asearch.php?' . $query_call . '"> next </a>'; 
    }

    echo '</h2></h4><hr> &nbsp';    

    for($j = get_post('start'); $j < min(get_post('start') + 48, $rows); ++$j)
    {
       //echo '<hr>';
       //echo 'Photographer: ' .mysql_result($result, $j, 'photographer') . '<br />';
       //echo 'Date: ' .mysql_result($result, $j, 'date') . '<br />';
       echo '<a href=' .mysql_result($result, $j, 'url') . ' padding: 0em>';
       //echo '<img src="http://lcweb2.loc.gov/service/pnp/fsac/1a34000/1a34300/1a34309r.jpg" height="228" />';
       echo '<img style="padding: 5px; border: 1px solid #000000; background #4A7491;" src="' .
           mysql_result($result, $j, 'picurl') . '" height="150" width="150" />';
       echo '</a>';
       if(($j - get_post('start')) % 8 == 7) echo '<br /> &nbsp';
    }
    echo '</center>';

}

mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_GET[$var]);
}

echo <<<_END
<br/>
</div>



</body>

</html>
_END;


?>
